<?php
// 用户模型
class UserModel extends Model {
	
	/**
	 * 绑定手机
	 * @param String $mobile
	 */
	public function bindMobile($mobile){
		$mid = $_SESSION['mid'];
		$map['mobile']=$mobile;
		$no=$this->where('id='.$mid)->save($map);
		if($no){
			$_SESSION['userInfo']['mobile']=$mobile;
			//取消其他账号的绑定
			$map['mobile']=$mobile;
			$map1['mobile']='';
			$this->where("mobile='".$mobile."' and id!=".$mid)->save($map1);
		}
		return $no;
	}
	
	
	/**
	 * 取消手机绑定
	 */
	public function unBindMobile(){
		$mid = $_SESSION['mid'];
		$map['mobile']='';
		$no=$this->where('id='.$mid)->save($map);
		if($no)$_SESSION['userInfo']['mobile']='';
		return $no;
	}
	
	/**
	 * 根据查询条件查询用户
	 * 
	 * @param array|string $map          查询条件
	 * @param string       $field		   字段
	 * @param int 		   $limit		   限制条数
	 * @param string 	   $order		   结果排序
	 * @param boolean 	   $is_find_page 是否分页
	 * @return array
	 */
	public function getUserByMap($map = array(), $field = '*', $limit = '', $order = '', $is_find_page = true) {
		if ($is_find_page) {
			return $this->where($map)->field($field)->order($order)->findPage($limit);
		}else {
			return $this->where($map)->field($field)->order($order)->limit($limit)->findAll();
		}
	}

	/**
	 * 获取用户列表
	 * 
	 * @param array|string $map             查询条件
	 * @param boolean	   $show_dept		是否显示部门信息
	 * @param boolean 	   $show_user_group 是否显示用户组
	 * @param string       $field		           字段
	 * @param string 	   $order		           结果排序
	 * @param int 		   $limit		 	限制条数
	 * @return array
	 */
    public function getUserList($map = '', $show_dept = false, $field = '*', $order = 'id ASC', $limit = 30) {
    	$res  = $this->where($map)->field($field)->order($order)->findPage($limit);
    	//$uids = getSubByKey($res['data'], 'id');
    	
    	//部门信息
    	//if ($show_dept) {
    	//}
    	
    	//用户组
    	/**
    	if ($show_user_group) {
    		$temp_user_group = model('UserGroup')->getUserGroupByUid($uids);

    		//转换成array($uid => $user_group)的格式
    		$user_group = array();
    		foreach($temp_user_group as $v) {
    			$user_group[$v['uid']][] = $v;
    		}
    		unset($temp_user_group);
    		
    		//将用户组信息添加至结果集
    		foreach($res['data'] as $k => $v) {
				$res['data'][$k]['user_group'] = isset($user_group[$v['uid']]) ? $user_group[$v['uid']] : array();
    		}
    	}
    	**/
    	return $res;
    }
    
	
    /**
     * 判断与第三方是否同步。
     * @param String $uid	用户ID
     * @param String $type	第三方类型
     */
    public function isThirdSync($uid,$type) {
		//if (!isset($this->_is_sina_sync)) {
		$map['uid']					= $uid;
		$map['type']				= $type;
		$map['oauth_token']			= array('neq', '');
		$map['oauth_token_secret']	= array('neq', '');
//			$map['is_sync']				= '1';
		$is_sync = M('login')->where($map)->find() ? true : false;
		//}
		return $is_sync;
		//设置绑定的账号
		$bind = M('login')->where('uid='.$uid)->findall();
        foreach ($bind as $v){
        	$data[$v['type']] = $v['is_sync'];
        }
        
        $_SESSION['login_bind']	= $data;
        dump($data);
	}
	
/**
     * 判断与第三方是否同步。
     * @param String $uid	用户ID
     * @param String $type	第三方类型
     */
    public function syncStatus($uid) {
		//设置绑定的账号
		$bind = M('login')->where('uid='.$uid)->findall();
        foreach ($bind as $v){
        	$data[$v['type']] = $v['is_sync'];
        }
        //dump($data);
        return $data;
	}
	
	
	//检查Email是否可用
	public function isEmailAvailable($email = null) {
		if(empty($email)){
			return false;
		}
		$map['email']=$email;
		$res = $this->where("`email`='".$email."'")->find();
		
		if ( !$res ) {
			//if ($return_type === 'ajax') echo 'success';
			//else 
			return true;
		}else {
			//if ($return_type === 'ajax') echo '邮箱已注册';
			//else 
			return false;
		}
	}
	
	
	/**
	 * 查找用户电话薄
	 * @param unknown_type $map
	 * @param unknown_type $field
	 * @param unknown_type $limit
	 * @param unknown_type $order
	 * @param unknown_type $is_find_page
	 */
	public function selectPhoneBookByMap($map = array(), $limit = '', $order = '', $is_find_page = true) {
		if ($is_find_page) {
			return M('Phonebook')->kPageQuery($map,$order,$limit);
			//where($map)->field($field)->order($order)->findPage($limit);
		}else {
			$dao = M('Phonebook')->where($map);
			if(!empty($order))$dao->order($order);
			if(!empty($limit))$dao->limit($limit);
			return $dao->findAll();
		}
	}
	
	
	public function selectPhoneBook4KeyValue($map = array()){
		$pbooks=$this->selectPhoneBookByMap($map,'','', false);
		foreach($pbooks as $vo){
			$result[$vo['mobile']]=$vo['name'];
		}
		return $result;
	}
	
	/**
	 * 添加用户电话薄
	 * @param unknown_type $ownerid
	 * @param unknown_type $mobile
	 * @param unknown_type $uname
	 */
	public function addPhoneBook($ownerid,$mobile,$name=''){
		$map['ownerid']=$ownerid;
		$map['mobile']=$mobile;
		$phonebook = M('Phonebook')->where($map)->find();
		
		if(!empty($phonebook)){
			//已存在
			$map['name']=$name;
			$map['is_delete']='N';
			$map['mtime']=time();
			$result = M('Phonebook')->where("`id`=".$phonebook['id']."")->save($map);
		}else{
			$map['name']=$name;
			$map['is_delete']='N';
			$map['ctime']=time();
			$result = M('Phonebook')->add($map);
		}
		return $result;
	}
	
	public function getPhoneBook($ownerid,$mobile){
		$map['ownerid']=$ownerid;
		$map['mobile']=$mobile;
		$map['is_delete']='N';
		$phonebook = M('Phonebook')->where($map)->find();
		return $phonebook;
	}
	
	/**
	 * 通过ID删除电话薄记录
	 * @param unknown_type $id
	 */
	public function deletePhoneBook($ownerid,$mobile){
		if(empty($ownerid))return 0;
		if(empty($mobile))return 0;
		$map['ownerid']=$ownerid;
		$map['mobile']=$mobile;
		$result = M('Phonebook')->where($map)->setField('is_delete','Y');
		return $result;
	}
	
	/**
	 * 登陆用户查看公开的用户
	 */
	public function getById($uid){
		$map['id']=$uid;
		$map['enable']='Y';
		$map['is_active']=1;
		//$map['role']=$role;
		$userinfo=$this->where($map)->find();
		return $userinfo;
	}
	
}
?>